﻿var zombies = zombies || {};

zombies.statisticDetail = function () {
    var self = this;
    self.Details = ko.observableArray();
    self.FromDate = ko.observable("");
    self.ToDate = ko.observable("");

    self.PreFrom = ko.observable();
    self.PreTo = ko.observable();

    self.doChangeFrom = function () {
        if (self.PreFrom() != self.FromDate()) {
            self.PreFrom(self.FromDate());
            self.doingChange();
        }
    }

    self.doChangeTo = function () {
        if (self.PreTo() != self.ToDate()) {
            self.PreTo(self.ToDate());
            self.doingChange();
        }
    }

    self.doingChange = function () {
        if (self.FromDate() != "" && self.ToDate() != "") {
            $.ajax({
                url: '/api/Statistic/getDetailsStatistic',
                type: 'GET',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: {
                    FromDate: self.FromDate,
                    ToDate: self.ToDate
                },
            }).done(function (items) {
                self.Details.removeAll();
                for (i in items) {
                    if (items.hasOwnProperty(i)) {
                        self.Details.push(new zombies.Statistic_Details(items[i]))
                    }
                }
            }).error(function () {
                toastr.error('Lỗi gì đó');
            })
        }
    }
};

var statisticModel;
$(document).ready(function () {
    statisticModel = new zombies.statisticDetail();
    var time = new Date($.now());
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var day = time.getDate();
    statisticModel.FromDate(month+ "-1-" + year);
    statisticModel.ToDate(month + "-" + day + "-" + year);
    statisticModel.doingChange();

    ko.applyBindings(statisticModel, document.getElementById("admin_content"));
});